//$Id$
(function ($) {
  Drupal.behaviors.maryville_customizations = {
  attach: function (context, settings) {
  jQuery(document).ready(function(){ 

    /*$("#block-menu-menu-patients-menu a").each(function () {
      $(this).attr("href",Drupal.settings.basePath+"node/add/appointments");
    });*/ 
   
    
    /*$(function(){
      $('#time-slots-wrapper').jScrollPane(
       {
	  showArrows: true,	
       }
      );

    }); */
   

    
  
    var today = new Date();

    $('#edit-field-appointment-date-und-0-value-datepicker-popup-0').datepicker({   //Enter your field id
      showOn: "both",
      buttonImage: Drupal.settings.basePath+"sites/all/themes/maryville/images/calendar.gif",
      buttonImageOnly: true,
      minDate: new Date(),
      maxDate: '+7d',
    });    
    

    $('#edit-field-appointment-date-und-0-value-datepicker-popup-0').autofill({
      value: 'SELECT DATE', 
      defaultTextColor: '#492D2D',
      activeTextColor: '#492D2D'           
    });

    $('#edit-field-appointment-date-und-0-value-datepicker-popup-0').val($("#edit-slot-selected-date").val());
   /*if ($(".selected-date-on-pageload").length > 0){
     $('#edit-field-appointment-date-und-0-value-datepicker-popup-0').val($(".selected-date-on-pageload").val());
   }*/ 

   $('#edit-appointments-filter-bydates').change(function(){
     $url = window.location.href.split("?");
     window.location.href= $url[0]+'?sel_date='+$(this).val();
  }); 
 
  jQuery("#edit-field-appointment-doctor-und").live('change', function() {
    $url = window.location.href.split("?");
    var date_selected = jQuery("#edit-field-appointment-date-und-0-value-datepicker-popup-0").val() != 'SELECT DATE' ? jQuery("#edit-field-appointment-date-und-0-value-datepicker-popup-0").val() : '';
    window.location.href= $url[0]+'?doctor_id='+jQuery(this).val()+'&sel_date='+date_selected;
    /*
    jQuery('#time-slots-wrapper').html('Please wait...');
    jQuery("#appointments-node-form #edit-title").val(jQuery("#edit-field-appointment-doctor-und :selected").text());        
    jQuery.ajax({      
      url: Drupal.settings.basePath+'get_time_slots',
      data: "did="+jQuery(this).val()+"&date="+jQuery("#edit-field-appointment-date-und-0-value-datepicker-popup-0").val()+"&dname="+jQuery("#edit-field-appointment-doctor-und :selected").text(),
      success: function(msg){
        jQuery('#time-slots-wrapper').html(msg);        
      }
    }); */
  
  });   
  

  jQuery("#edit-field-appointment-date-und-0-value-datepicker-popup-0").live('change',function(){
    $url = window.location.href.split("?");
    var date_selected = jQuery(this).val() != 'SELECT DATE' ? jQuery(this).val() : '';
    window.location.href= $url[0]+'?doctor_id='+jQuery("#edit-field-appointment-doctor-und :selected").val()+'&sel_date='+date_selected;
    /*
    jQuery('#time-slots-wrapper').html('Please wait...');
    jQuery.ajax({                
      url: Drupal.settings.basePath+'get_time_slots',
      data: "did="+jQuery("#edit-field-appointment-doctor-und :selected").val()+"&date="+jQuery(this).val()+"&dname="+jQuery("#edit-field-appointment-doctor-und :selected").text(),
      success: function(msg){        
        jQuery('#time-slots-wrapper').html(msg);        
      }
    });
    */

  }); 

  $('.shchedule_appointment_form #edit-submit').click(function() {
     jQuery('#edit-field-appointment-date-und-0-value-datepicker-popup-0').val('');
  });  

  /*
   * When click on the time slot, get the date and doctor id and assign to appropriate fields.
   */

  $('[class^=available-time-slot-]').live('click',function() {               
    var seltime = $(this).html(); 
    $('#slot-confirm-message').html('You have an appointment with '+$(this).parents('.time-slots').attr('doctor_name')+' on '+$(this).parents('.time-slots').attr('slotsdate')+' at '+seltime);
    $('.selected-date-on-pageload').val($(this).parents('.time-slots').attr('slotsdate'));
    $('.selected-doctor-on-pageload').val($(this).parents('.time-slots').attr('doctor_id'));
    $('input#edit-field-appointment-time-und-0-value').val(seltime);            
    $("#appointments-node-form #edit-title").val('Appointment with '+$(this).parents('.time-slots').attr('doctor_name')); 
    $('.time-slots ul').children().removeClass("selected");    
    $(this).addClass('selected');
    var seltime = $(this).html();
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height:300,
      width:400,
      modal: true,
 	buttons: {
   	  "Confirm": function() {            
            $('.time-slots ul').children().removeClass("active"); 
            $('.time-slots ul li.selected').addClass("active");
	    $( this ).dialog( "close" );
	  },
	  Cancel: function() {
            $('input#edit-field-appointment-time-und-0-value').val('');
	    $( this ).dialog( "close" );
	  }
	}
    });
    
  });

  if($('#edit-field-appointment-date-und-0-value-datepicker-popup-0').val().length <= 0) {
    $('#edit-field-appointment-date-und-0-value-datepicker-popup-0').val('SELECT DATE'); 
  }
  
});  
}
  };

}(jQuery));
